package ru.nsu.effel.actions;

import javax.servlet.http.HttpSession;

import ru.nsu.effel.persistence.Account;
import ru.nsu.effel.persistence.AccountManager;
import ru.nsu.effel.persistence.ConnectionBid;
import ru.nsu.effel.persistence.ConnectionBidManager;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

public class ManagerProfileActionBean implements ActionBean{
	private ActionBeanContext context;
	private int bidId;
	private ConnectionBid bid;
	private int[] bidIds;
	
	public int[] getBidIds() {
		return bidIds;
	}

	public void setBidIds(int[] bidIds) {
		this.bidIds = bidIds;
	}

	public ConnectionBid getBid() {
		return bid;
	}

	public void setBid(ConnectionBid bid) {
		this.bid = bid;
	}

	public int getBidId() {
		return bidId;
	}

	public void setBidId(int bidId) {
		this.bidId = bidId;
	}

	public ActionBeanContext getContext() {
		return context;
	}

	public void setContext(ActionBeanContext context) {
		this.context = context;
	}
	
	public Resolution connectionBidInfo() {
		this.bid = ConnectionBidManager.getInstance().getConnectionBidById(bidId);
		return new ForwardResolution("/ConnectionBidInfo.jsp");
	}
	
	public Resolution acceptBid() {
		this.bid = ConnectionBidManager.getInstance().getConnectionBidById(bidId);
		HttpSession session = getContext().getRequest().getSession(false);
		Integer managerId = (Integer) session.getAttribute("accountId");
		AccountManager.getInstance().addAccount(new Account(bid.getFirstName(), bid.getLastName(), "Client" , bid.getUsername(), bid.getPassword()));
		ConnectionBidManager.getInstance().addConnectionBidtoManager(managerId, bid.getId());
		bid.setBidStatus(1);
		ConnectionBidManager.getInstance().updateConnectionBid(bid);
		return new ForwardResolution("/ManagerProfile.jsp");
	}
	
	public Resolution sendBidsToFitter() {
		int fitterId = 6;
		for (int i = 0; i < bidIds.length; i++) {
			ConnectionBid conBid = ConnectionBidManager.getInstance().getConnectionBidById(bidIds[i]);
			conBid.setBidStatus(2);
			ConnectionBidManager.getInstance().updateConnectionBid(conBid);
			ConnectionBidManager.getInstance().addConnectionBidtoFitter(fitterId, bidIds[i]);
		}
		return new ForwardResolution("/ManagerProfile.jsp");
	}
	
	public Resolution acceptDoneBids() {
		HttpSession session = getContext().getRequest().getSession(false);
		Integer managerId = (Integer) session.getAttribute("accountId");
		ConnectionBidManager.getInstance().deleteConnectionBidsFromManager(managerId, bidIds);
		for(int i = 0; i <bidIds.length; i++) {
			ConnectionBidManager.getInstance().deleteConnectionBidsByIds(bidIds);
		}
		return new ForwardResolution("/ManagerProfile.jsp");
	}
}
